package admin.recordatorio;

import DBMS.DataBaseManager;
import admin.permisos.PermisosManager;
import clases.Recordatorio;
import clases.Usuario;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author jesus
 */
public class ConsultarRecordatorio extends DispatchAction {

    /*
     * forward name="success" path=""
     */
    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";
    private static final String name = "Consultar Recordatorio";

    public ActionForward all(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        Recordatorio recordatorio = (Recordatorio) form;
        DataBaseManager dbm = DataBaseManager.getInstance();

        ArrayList<Recordatorio> resultadoConsulta =
                dbm.consultarRecordatorios(recordatorio);

        if (resultadoConsulta.isEmpty()) {
            recordatorio.setMensaje("No se ha podido realizar la consulta.");
            request.setAttribute("recordatorio", recordatorio);
            return mapping.findForward(FAILURE);
        }

        recordatorio.setMensajeOK("Consulta realizada con exito.");
        request.setAttribute("recordatorios", resultadoConsulta);
        return mapping.findForward(SUCCESS);
    }
    /*
    public ActionForward populate(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    
    return mapping.findForward(SUCCESS);
    }
     * */
}
